package net.roseparadise.controller;

import java.util.List;

import net.roseparadise.model.Notice;
import net.roseparadise.service.NoticeService;

import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/action/notice.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "NoticeController"), name = "NoticeAjaxController")   

//@SessionAttributes("currUser")
public class NoticeController {
	
	@Autowired
	private NoticeService noticeService;

	@RequestMapping(params = "cmd=add",method= RequestMethod.POST)
	public String addNotice(String title,ModelMap map){
		//System.out.println("~~~~~~~~Controller begin~~~~~~~~"+title);
		Notice notice = new Notice(1,"Y",title,"20091229000000");	
		noticeService.save(notice);
		List list = noticeService.listStudioNotice();
		map.addAttribute("noticelist", list);
		return "noticelist";
	}
	
	@RemoteMethod 
	public Notice addNoticeAjax(Notice notice){
		notice.setMemberid(1);
		notice.setCreatedate("20091229000000");
		notice.setUpdatedate("");
		//System.out.println(notice.getIsstudio());
		List list=null;
		try {
			noticeService.save(notice);
			list = noticeService.listStudioNotice();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		Notice result=(list!=null && list.size()>0)?(Notice)list.get(0):notice;
		return result;
	}
	
	@RemoteMethod 
	public int getNoticeNumber(){		
		List list = noticeService.listStudioNotice();
		//System.out.println("~~~~~~~~NoticeDWR begin~~~~~~~~"+list.size());		
		return list.size();
	}

}
